/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cnsatmsmart.automation.mmi.model;

import com.bbn.openmap.BufferedLayerMapBean;
import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.Layer;
import com.bbn.openmap.LayerHandler;
import com.bbn.openmap.MapHandler;
import com.bbn.openmap.MouseDelegator;
import com.bbn.openmap.event.AbstractMouseMode;
import com.bbn.openmap.event.SelectMouseMode;
import com.bbn.openmap.layer.GraticuleLayer;
import com.bbn.openmap.layer.shape.ShapeLayer;
import java.awt.Color;
import java.util.Properties;

/**
 *
 * @author Rinz
 */
public class OMMapPanel extends BufferedLayerMapBean {

    /*atribut-atribut kelas OMMapPanel*/
        protected MapHandler mapHandler;        //untuk menangani panel peta
        protected LayerHandler layerHandler;    //untuk menangani layer-layer yang berada diatas panel peta
        private LatLonPoint centerLatLon;       //titik tengah dari panel peta yang didasarkan pada latitude dan longitude
        private float scale;                    //skala dari peta
        private GraticuleLayer gratLayer;       //layer untuk garis lintang dan garis bujur
        private Color backgroundColor = new Color(0,111,111);          //warna latar dari peta
        private AbstractMouseMode mouseMode;    //menangani penggunaan mouse pada peta

    /*constructor*/
        public OMMapPanel()
        {
            this.scale = 17000000f; //supaya peta bisa tertampilkan semua di satu layar monitor
            this.centerLatLon = new LatLonPoint(-2.0f, 118.0f); //center of indonesia

            /*** Map Handler ***/
            this.mapHandler = getMapHandler(); //jika belum ada, dibuat terlebih dahulu. jika sudah pernah ada, diambil
            this.mapHandler.add(this); //untuk menambahkan objek pada mapHandler

            /*** Map ***/
            suppressCopyright = true;
            setCenter(this.centerLatLon);
            setBackground(this.backgroundColor);
            setScale(this.scale);

                //*** Graticule layer ***/
                this.gratLayer = new GraticuleLayer();
                this.gratLayer.setVisible(true);

                //*** Radar layer ***/
                

            /*** Layer Handler ***/
            this.layerHandler = new LayerHandler();
            this.layerHandler.addLayer(indonesiaLayer()); //*** Indonesia map layer ***/
            this.layerHandler.addLayer(this.gratLayer);
            this.mapHandler.add(this.layerHandler);

            /*** mouse mode ***/
            this.mapHandler.add(new MouseDelegator()); 
            this.mouseMode = new SelectMouseMode();
            setMouseMode(this.mouseMode);
        }

    /*** other method ***/
        private MapHandler getMapHandler()
        {
            if (mapHandler == null)
                mapHandler = new MapHandler();

            return mapHandler;
        }

        private Layer indonesiaLayer()
        {
            ShapeLayer shapeLayer = new ShapeLayer();
            Properties shapeLayerProp = new Properties();
            shapeLayerProp.put("prettyName", "Indonesia Provinces");
            shapeLayerProp.put("lineColor","C0C0C0");
            shapeLayerProp.put("shapeFile","data/shape/ind_provinces.shp");
            shapeLayer.setProperties(shapeLayerProp);

            return shapeLayer;
        }

        public void setMouseMode(AbstractMouseMode mouseMode)
        {
            if (this.mapHandler == null)
                return;

            if (this.mouseMode!= null)
                this.mapHandler.remove(this.mouseMode);

            this.mouseMode = mouseMode;
            this.mapHandler.add(mouseMode);
        }

        public void setCenterAndFit()
        {
            setCenter(this.centerLatLon);
            setScale(this.scale);
        }
}
